<?php
namespace app\admin\controller;

use app\model\admin;
use think\Request;
use think\Session;
use think\Controller;

class Tags extends Controller
{
    public function index()
    {
        $dao = new \app\model\admin\Tags();
        $where=[];
        $lists = $dao->get_tags_lists($where,$sort='id desc',$paginate=10);
        $this->assign('lists', $lists);
        return $this->fetch('tags/index');
    }
    //tag详情
    public function detail()
    {
        $dao = new \app\model\admin\Tags();
        $id = $request = $this->request->get('id');
        $where['id']=$id;
        if(empty($id)){
            $data = array();
        }else{
            $data = $dao->tags_detail($where);
        }
        return $this->fetch('tags/detail',$data);
    }

    //tags添加修改
    public function tags_save()
    {
        $dao = new \app\model\admin\Tags();
        $data['id'] = deletehtml($this->request->post('id'));
        $data['title'] = deletehtml($this->request->post('title'));
        $data['keywords'] = deletehtml($this->request->post('keywords'));
        $data['describe'] = deletehtml($this->request->post('describe'));
        $data['status'] = deletehtml($this->request->post('status'));
        $data['sort'] = deletehtml($this->request->post('sort'));
        if(empty($data['id'])){
            $data['add_time'] = time();
            $advert_status = $dao->add_tags($data);
        }else{
            $where['id'] = $data['id'];
            $tags_status = $dao->update_tags($where,$data);
        }
        if($tags_status){
            $this->redirect('/admin/tags/index');
        }else{
            $this->error('操作失败！', '/admin/tags/detail?id='.$data['id']);
        }
    }

    //tags添加
    public function tags_add()
    {
        if (Request::instance()->isAjax()){
            $dao = new \app\model\admin\Tags();
            $title = $request = $this->request->post('title');
            $where['title']=$title;
            $data = $dao->tags_detail($where);
            if($data)return json(['status'=>0,'data'=>(object)[],'msg'=>'添加成功!']);
            $data['keywords'] = $data['name']=$name;
            $data['describe'] = '这里是'. $data['name'].'标签,感谢大家的关注！';
            $data['status']=1;
            $data['add_time'] = time();
            $tags_status = $dao->add_tags($data);
            if($tags_status){
                return json(['status'=>0,'data'=>(object)[],'msg'=>'添加成功!']);
            }else{
                return json(['status'=>1000,'data'=>(object)[],'msg'=>'操作失败!']);
            }
        }
        return json(['status'=>1000,'data'=>(object)[],'msg'=>'err!']);
    }

    //改变用户状态
    public function tags_status()
    {
        $dao = new \app\model\admin\Tags();
        if($this->request->get("id")<1){
            return json(['status'=>2,'data'=>(object)[],'msg'=>'参数错误!']);
        }else{
            $id = $this->request->get("id");
        }
        $status = $this->request->get("status");
        if(!in_array($status,[1,2]))
        {
            return json(['status'=>2,'data'=>(object)[],'msg'=>'参数错误!']);
        }
        $where['id'] = $id;
        $data['status'] = $status;
        $status = $dao->update_tags($where,$data);
        if($status > 0){
            return json(['status'=>0,'data'=>(object)[],'msg'=>'操作成功!']);
        }else{
            return json(['status'=>3,'data'=>(object)[],'msg'=>'操作失败!']);
        }
    }

    //删除广告
    public function tags_delete()
    {
        $dao = new \app\model\admin\Tags();
        if($this->request->get("id")<1){
            return json(['status'=>2,'data'=>(object)[],'msg'=>'参数错误!']);
        }else{
            $id = $this->request->get("id");
        }
        $advert_delete = $dao->delete_tags([$id]);
        if($advert_delete == 0){
            return json(['status'=>3,'data'=>(object)[],'msg'=>'删除失败!']);
        }else{
            return json(['status'=>0,'data'=>$advert_delete,'msg'=>'操作成功!']);
        }
    }
}


